package com.andy.module.ext

import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.andy.module.R
import com.andy.module.widget.GridDecoration
import com.andy.module.widget.VerticalItemDecoration
import com.andy.ui.ext.dp2px

/**
 * @creator:LuoFan
 * @date:2022/6/7
 * @description:
 */

fun RecyclerView.initVerticalLayout() {
    val layoutManager = LinearLayoutManager(this.context)
    layoutManager.orientation = LinearLayoutManager.VERTICAL
    this.layoutManager = layoutManager
}

fun RecyclerView.initHorizontalLayout() {
    val layoutManager = LinearLayoutManager(this.context)
    layoutManager.orientation = LinearLayoutManager.HORIZONTAL
    this.layoutManager = layoutManager
}

fun RecyclerView.initGridLayout(count: Int) {
    val layoutManager = GridLayoutManager(this.context, count)
    this.layoutManager = layoutManager
}

fun RecyclerView.initDividerLine(
    verticalSpace: Int = 1,
    horizontalMargin: Int = dp2px(16)
) {
    val color = ContextCompat.getColor(this.context, R.color.line_divider)
    val divider = VerticalItemDecoration(verticalSpace, color)
    divider.setHasBottomDivider(false)
    divider.setHorizontalMargin(horizontalMargin)
    this.addItemDecoration(divider)
}

fun RecyclerView.initGridDividerLine(
    verticalSpace: Int = dp2px(8),
    horizontalSpace: Int = dp2px(8),
    count: Int
) {
    val divider = GridDecoration()
    divider.verticalSpace = verticalSpace
    divider.horizontalSpace = horizontalSpace
    divider.columnCount = count
    this.addItemDecoration(divider)
}
